/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threadclient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bryce
 */
public class Client {

    private String IP, string;
    private int port, numSends;
    private Socket socket;
    private ClientLogger logger;
    private int clientId;
    private int sleepTime;

    public Client(String IP, int port, String string, int numSends, ClientLogger logger, int sleepTime, int clientId) {
        this.IP = IP;
        this.port = port;
        this.string = string;
        this.numSends = numSends;
        this.logger = logger;
        this.sleepTime = sleepTime;
        this.clientId = clientId;
    }

    public void establishConnection() {
        InetAddress address;
        try {
            address = InetAddress.getByName(this.IP);
        } catch (UnknownHostException ex) {
            logger.addItem("Unknown host exception: ");
            return;
        }
        try {
            socket = new Socket(address, this.port);
        } catch (IOException ex) {
            logger.addItem(new Date().getTime()+",connection failure");
        }
    }

    public void performOperations() {
        byte[] data = new byte[this.string.getBytes().length];
        long milliStart = 0;
        long milliFin = 0;
        try {
            // Perform a number of send / rec functions
            for (int i = 0; i < numSends; i++) {
                //Send data
                try {
                    //If this is the last send, indicate that to the server.
                    
                    milliStart = new Date().getTime();
                    socket.getOutputStream().write(this.string.getBytes());
                    socket.getOutputStream().flush();
                } catch (IOException ex) {
                    logger.addItem(new Date().getTime()+",send failure,"+socket.getLocalPort());
                    try {
                        socket.close();
                        return;
                    } catch (IOException ex1) {
                    }
                }
                // Now wait for a response
                try {
                    socket.getInputStream().read(data);
                    milliFin = new Date().getTime();
                } catch (IOException ex) {
                    logger.addItem(new Date().getTime()+",read failure,"+socket.getLocalPort());
                }
                logger.addItem(new Date().getTime()+","+this.clientId+","+i+","+(milliFin - milliStart));
                //Pause before the next send/rec cycle
                Thread.sleep(sleepTime);
            }
            try {
                // Close down the socket.
                socket.close();
            } catch (IOException ex) {
                return;
            }
            
        } catch (InterruptedException ie) {
            return;
        }
    }
}
